package com.chenc.javathreads.controlleraop.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.chenc.javathreads.controlleraop.dto.ApiDto;
import com.chenc.javathreads.controlleraop.dto.UsualParam;
import com.chenc.javathreads.controlleraop.signutil.CaiNiaoSignUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName
 * @Describtion: gatewaydemo  与外部交互  规定一定的额数据结构进行签名验签
 * 、
 *
 * 规定传入数据和传出数据
 * @Author:Chenc
 * @Date:2023/12/30 21:30
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/cqk/api")
public class ApiOutController {

    @PostMapping("/test")
    public String demoTestApi(@RequestBody ApiDto apiDto){

        String sign = CaiNiaoSignUtil.sign(apiDto.getJsonBody(), apiDto.getTimeStamp());
        String signest = apiDto.getSign();
        if (sign.equals(signest)){
            return "sign approve";
        }else{
            return "sign reject";
        }
    }

    @PostMapping("/normal")
    public String normalApi(@RequestBody ApiDto apiDto, HttpServletRequest httpServletRequest){
        //网关接受做转发
        log.info("");
       return "";
    }

    public static void main(String[] args) {
        UsualParam build = UsualParam.builder()
                .param1("param1")
                .param2("param2")
                .param3("param3").build();
        String secret = "timestamp";
        String jsonStr = JSONUtil.toJsonStr(build);
        String sign = CaiNiaoSignUtil.sign(jsonStr, secret);
        ApiDto build1 = ApiDto.builder()
                .appApiCode("appApiCode")
                .appCode("appCode")
                .jsonBody(jsonStr)
//                .sign(sign)
                .timeStamp(secret)
                .build();
        String jsonStr1 = JSONUtil.toJsonStr(build1);
        System.out.println(jsonStr1);
    }

}
